import * as Api from "./api/login";
import * as CommonApi from "./api/common";
import common, { getApiUrl } from "./utils/common";
App({
  onLaunch(options) {
    // 展示本地存储能力
    const logs = wx.getStorageSync("logs") || [];
    logs.unshift(Date.now());
    wx.setStorageSync("logs", logs);
    common.init(this);
    const _this = this;
    this.globalData.wxScene = options.scene;

    Api.getCheckLogin(_this).then((res) => {
      this.globalData.is_login = res.is_login;
      wx.setStorageSync("is_login", res.is_login);
      console.log("is_login_______", res.is_login);
      console.log("options.scene1", options.scene);
      if (
        options.scene === 1047 ||
        options.scene === 1048 ||
        options.scene === 1049
      ) {
        wx.setStorageSync("wxScene", options.scene);
        console.log("options.scene2", options.scene);
        // 扫码进入场景
        const param = options.query.scene || ""; // 获取二维码参数
        _this.globalData.param = param; // 保存二维码携带参数
        if (!res.is_login) {
          console.log("options.scene3", options.scene);
          wx.navigateTo({
            url: `/pages/index/index?cabinet_number=${param}`,
          });
        } else {
          console.log("options.scene4", options.scene);

          wx.navigateTo({
            url:
              "/pages/cabinetInfo/cabinetInfo?cabinet_number=" +
              _this.globalData.param,
          });
        }

        // 登录
        // wx.login({
        //   success(res) {
        //     console.log(res);
        //     console.log(Api);
        //     _this.globalData.code = res.code;

        //     // Api.getTokenCreate(_this, res.code).then((create) => {
        //     //   console.log(create);
        //     //   const { is_bind, token } = create;
        //     //   _this.globalData.token = token;
        //     //   _this.globalData.isBind = is_bind;
        //     //   wx.setStorageSync("token", token);
        //     //   wx.setStorageSync("isBind", is_bind);
        //     //   wx.navigateTo({
        //     //     url:
        //     //       "/pages/cabinetInfo/cabinetInfo?cabinet_number=" +
        //     //       _this.globalData.param,
        //     //   });
        //     // });
        //   },
        // });
      }
    });
    CommonApi.getCustomerInfo(_this).then((res) => {
      console.log("res", res);
      this.globalData.kf = res;
    });
  },
  globalData: {
    userInfo: null,
    code: "",
    token: "",
    isBind: false,
    agreement: getApiUrl() + "/agreement.html",
    kf: {},
    wxScene: "",
    is_login: false,
  },
  handleKf() {
    const { link, corpid } = this.globalData.kf;
    wx.openCustomerServiceChat({
      extInfo: { url: link },
      corpId: corpid,
      success(res) {
        console.log("res", res);
      },
      fail(err) {
        console.log("err", err);
      },
    });
  },
});
